package com.springMVCStudy.cookie;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("cookie")
@ResponseBody
public class CookieController {

    // 先访问save将相应的cookie存到浏览器中，再访问data
    @RequestMapping("data")
    public String data(@CookieValue(value = "cookieName") String value) {
        System.out.println("value: " + value);
        return "value: " + value;
    }

    /*
    @GetMapping是@RequestMapping的一个特例，专门用来处理HTTP GET请求。
    它隐式地将method属性设置为RequestMethod.GET，因此你不需要显式地指定请求类型。
    */
    @GetMapping("save")
    public String save(HttpServletResponse response) {
        Cookie cookie = new Cookie("cookieName", "cookieValue");
        response.addCookie(cookie);
        System.out.println("cookie: " + cookie.getValue());
        return "cookie(" + cookie.getName() + ", " + cookie.getValue() + ") set succesfully!!!";
    }


}
